package forms;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JLookAndFeelMenu extends JPopupMenu {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	private static final String defaultMenuName = "LookAndFeel";

	
	UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();

	
	ButtonGroup buttonGroup = new ButtonGroup();

	
	Component parent;

	

	public JLookAndFeelMenu(Component parent) {

	}

	public JLookAndFeelMenu() {
		super(defaultMenuName);
	}

	
	private void generateLookAndFeelSubMenu(final Component parent) {
		for (int i = 0; i < info.length; i++) {
			JRadioButtonMenuItem item = new JRadioButtonMenuItem(info[i].getName(), i == 0);
			final String className = info[i].getClassName();
			
			item.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evn) {
					try {
						UIManager.setLookAndFeel(className);
					} catch (Exception e) {
						e.printStackTrace();
					}

					SwingUtilities.updateComponentTreeUI(parent);
				}
			});
			buttonGroup.add(item);
			add(item);
		}
	}

	public Component getParentComponent() {
		return parent;
	}

	public void setParentComponent(Component parent) {
		this.parent = parent;
		generateLookAndFeelSubMenu(parent);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 300);
		frame.setLocation(200, 200);
		JMenuBar bar = new JMenuBar();
		JLookAndFeelMenu menu = new JLookAndFeelMenu();
		menu.setParentComponent(frame);
		bar.add(menu);
		frame.getContentPane().add(BorderLayout.NORTH, bar);
		frame.getContentPane().add(BorderLayout.CENTER, new JButton("Hello"));
		frame.setVisible(true);
	}

}